package io.shallwe._admin.line;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageConfig;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.CharacterSetECI;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

import static io.shallwe.scanner.manual.MessagePacket.CHARSET;

/**
 * @Description:  一维码与二维码生成工具类
 * @author: gkw
 * @description: io.shallwe._admin.line
 * @date:2019/9/17
 **/
public class QRCodeKit {

    /**
     * @Description:   生成二维码
     * @param:  [contents, width, height, margin]
     * @return: java.awt.image.BufferedImage
     * @author : gkw
     */
    public BufferedImage createQRCode(String contents, int width, int height, int margin){
        if (contents == null || contents.equals("")) {
//            throw new Exception("contents不能为空。");
            return null;
        }
        // 二维码基本参数设置
        Map<EncodeHintType, Object> hints = new HashMap<>();
        // 设置编码字符集utf-8
        hints.put(EncodeHintType.CHARACTER_SET, CharacterSetECI.UTF8);
        // 设置纠错等级L/M/Q/H,纠错等级越高越不易识别，当前设置等级为最高等级H
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
        // 可设置范围为0-10，但仅四个变化0 1(2) 3(4 5 6) 7(8 9 10)
        hints.put(EncodeHintType.MARGIN, margin);
        // 生成图片类型为QRCode
        BarcodeFormat format = BarcodeFormat.QR_CODE;
        // 创建位矩阵对象
        BitMatrix matrix = null;

        try {
            // 生成二维码对应的位矩阵对象
            matrix = new MultiFormatWriter().encode(contents, format, width, height, hints);
        } catch (WriterException e) {
            e.printStackTrace();
        }
        // 设置位矩阵转图片的参数
        MatrixToImageConfig config = new MatrixToImageConfig(Color.black.getRGB(), Color.white.getRGB());
        // 位矩阵对象转BufferedImage对象
        BufferedImage qrcode = MatrixToImageWriter.toBufferedImage(matrix, config);
        return qrcode;
    }

    /**
     * @Description:  生成一维码
     * @param:  [contents, width, height]
     * @return: java.awt.image.BufferedImage
     * @author : gkw
     */
    public BufferedImage createBarCode(String contents, int width, int height){
        if (contents == null || contents.equals("")) {
//            throw new Exception("contents不能为空。");
            return null;
        }

        Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
        hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
        // 创建位矩阵对象
        BitMatrix matrix = null;
        try {
            matrix = new MultiFormatWriter().encode(contents,
                    BarcodeFormat.CODE_128, width, height, hints);
        }catch (WriterException e){
            e.printStackTrace();
        }

        // 设置位矩阵转图片的参数
        MatrixToImageConfig config = new MatrixToImageConfig(Color.black.getRGB(), Color.white.getRGB());
        // 位矩阵对象转BufferedImage对象
        BufferedImage barcode = MatrixToImageWriter.toBufferedImage(matrix, config);
        return barcode;
    }
}
